﻿Imports System.Windows.Forms
Public Class CRunEnv
    Private Shared uniqueInstance As CRunEnv = New CRunEnv

    Public Shared Property AppPath As String
    Public Shared Property DBConnection As CDBConnection
    Public Shared Property SystemType As String
    Public Shared Property AccountName As String
    Public Shared Property AccountDataBase As String
    Public Shared Property AccountServer As String
    Public Shared Property CurrentUser As Object


    Private Sub New()
        Dim fileName As String
        Dim mainStr As String
        Dim userID As String = ""
        Dim pwd As String = ""
        AppPath = Application.StartupPath
        fileName = AppPath & "\" & "system.ini"

        If Not My.Computer.FileSystem.FileExists(fileName) Then
            MsgBox("配置文件丢失" & fileName, MsgBoxStyle.Exclamation, "系统提示")
            Return
        End If
        mainStr = CToolFileIni.ReadKey("DataBase", "Account_System", fileName)
        unCompress(mainStr, userID, pwd)
        DBConnection = OpenNewCnnSqlServer("", "System", userID, pwd)
    End Sub

    Public Shared Function GetRunEnv() As CRunEnv
        Return uniqueInstance
    End Function

    Public Shared Function OpenNewCnnSqlServer(ByVal systemType As String, ByVal dataBase As String,
                                               ByVal userID As String, ByVal pwd As String) As CDBConnection
        Dim serverName As String
        If Len(systemType) = 0 Then
            dataBase = "System"
        End If
        serverName = GetServerName(systemType)
        DBConnection = New CDBConnection(serverName, dataBase, userID, pwd)
        Return DBConnection
    End Function

    Private Shared Function GetServerName(ByVal systemType As String) As String
        Dim serverName As String
        Dim fileName As String
        fileName = AppPath & "\" & "system.ini"
        If Len(systemType) = 0 Then
            systemType = "System"
        End If
        If Not My.Computer.FileSystem.FileExists(fileName) Then
            MsgBox("配置文件丢失", MsgBoxStyle.Exclamation, "系统提示")
            Return ""
        End If

        serverName = CToolFileIni.ReadKey("DataBase", "Server_" & systemType, fileName)
        Return serverName
    End Function


    Public Shared Function unCompress(ByVal mainStr As String, _
            ByRef userID As String, ByRef pwd As String) As Boolean
        Dim str1 As String, str2 As String
        Dim pos1 As Integer, pos2 As Integer
        Dim objCipher As New CCipher
        On Error GoTo err_1
        str1 = objCipher.token_DataCompress(mainStr, False)
        str1 = objCipher.token_DataCompress(str1, False)

        '1、帐号
        pos1 = InStr(1, str1, "UserID=")
        If pos1 = 0 Then GoTo err_1
        pos1 = pos1 + 7

        pos2 = InStr(pos1, str1, ";")
        If pos2 = 0 Then GoTo err_1

        userID = Mid(str1, pos1, pos2 - pos1)

        '2、密码
        pos1 = InStr(1, str1, "PWD=")
        If pos1 = 0 Then GoTo err_1
        pos1 = pos1 + 4

        pos2 = InStr(pos1, str1, ";")
        If pos2 = 0 Then GoTo err_1

        pwd = Mid(str1, pos1, pos2 - pos1)

        unCompress = True
        Exit Function
err_1:

    End Function
End Class
